//
//  ViewController.m
//  仿京东地址选择器
//
//  Created by up72_2017_new on 2018/6/22.
//  Copyright © 2018年 up72_2017_new. All rights reserved.
//

#import "ViewController.h"
#import "CGCommonSelectedView.h"
#import "MJExtension.h"

#define CFWeak(type)  __weak typeof(type) weak##type = type
@interface ViewController ()
// 种植地址选择控件
@property(nonatomic,strong) CGCommonSelectedView *addressSelectedView;
@property (weak, nonatomic) IBOutlet UILabel *addressLabel;
@end

@implementation ViewController
- (CGCommonSelectedView *)addressSelectedView {
    CFWeak(self);
    if (!_addressSelectedView) {
        _addressSelectedView = [[CGCommonSelectedView alloc] init];
        _addressSelectedView.title = @"选择地址";
        
        // 选中回调
        _addressSelectedView.clickBlock = ^(CGCommonItemModel *selectedModel, NSInteger shouldAddItemId) {
            
            [weakself initializeListDataWithShouldItemId:shouldAddItemId];
        };
        
        // 选择结束回调
        _addressSelectedView.selectedFinishBlock = ^(NSArray<CGCommonItemModel *> *selectedArr) {
            NSMutableString *address = [NSMutableString string];
            
            for (int i = 0; i < selectedArr.count; ++i) {
                CGCommonItemModel *model = selectedArr[i];
                [address appendString:[NSString stringWithFormat:@"%@\n", model.title]];
                
                if (i == selectedArr.count - 1) {
                    NSLog(@"-------------");
                }
            }
            
            weakself.addressLabel.text = address;
            
        };
        
        _addressSelectedView.defaultHeight = 350;       // 视图高度
        _addressSelectedView.titleScrollViewH = 37;     // 标题高度
        _addressSelectedView.titleMaxWidth = 200;
        [self.view addSubview:[_addressSelectedView initSelectedWidgetView]];  // 初始化
        
        // 初始化第一级数据
        [self initializeListDataWithShouldItemId:0];
    }
    return _addressSelectedView;
}


- (void)initializeListDataWithShouldItemId:(NSInteger)itemID {
    NSMutableArray *arr = [NSMutableArray arrayWithCapacity:1];
    if (itemID < 6) {
        for (int i = 0; i < 10; i++) {
            NSDictionary *dic = @{@"title":[NSString stringWithFormat:@"第%ld列，第%d行", itemID, i]};
            [arr addObject:dic];
        }
    }
    [self handleAddressMsgWith:arr];
}

/**
 处理获取到的种植地址数据
 
 @param addressArr 种植地址数据
 */
- (void)handleAddressMsgWith:(NSArray *)addressArr {
    if (addressArr.count == 0) {
        [self.addressSelectedView addDataWithModel:nil];
        return;
    }
    CGRootModel *rootModel = [[CGRootModel alloc] init];
    rootModel.itemArr = [CGCommonItemModel mj_objectArrayWithKeyValuesArray:addressArr];
    [self.addressSelectedView addDataWithModel:rootModel];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.addressSelectedView show];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
